package com.kongming.kmdm.ws.list;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

import javax.websocket.Session;

public class WSList {

    private List<Session> sessionList = new ArrayList<Session>();

    private Object listLock = new Object();

    private static Logger logger = LoggerFactory.getLogger(WSList.class);

    public void add(Session ssn) {

        synchronized (listLock) {
            sessionList.add(ssn);
            logger.info("add ssn {},size {}", ssn.getId(), sessionList.size());
        }
    }

    public void remove(Session ssn) {

        synchronized (listLock) {
            sessionList.remove(ssn);
            logger.info("remove ssn {},size {}", ssn.getId(), sessionList.size());
        }
    }

    public List<Session> getClone() {

        synchronized (listLock) {
            int length = sessionList.size();
            return sessionList.subList(0, length);
        }
    }

    public int getSsnSize() {
        return sessionList.size();
    }
}
